#include "RingQueue.hpp"
#include "Thread.hpp"
#include <string>
#include <vector>
#include <unistd.h>

using namespace std;
using namespace ThreadModule;

int a = 10;

using ringqueue_t = RingQueue<int>;

void Consumer(ringqueue_t &rq, const string &name)
{
    while (true)
    {
        sleep(4);
        int data = 0;
        rq.Pop(&data);
        cout << "Consumer: " << data << "[" << name << "]" << endl;
    }
}

void Productor(ringqueue_t &rq, const string &name)
{
    int cnt = 100;
    while (true)
    {
        sleep(1);
        rq.Enqueue(cnt);
        cout << "Productor: " << cnt-- << "[" << name << "]" << endl;
    }
}

void Start(vector<Thread<ringqueue_t>> *threads, int num, ringqueue_t &rq, func_t<ringqueue_t> func)
{
    for (int i = 0; i < num; i++)
    {
        // Thread(func_t<T> func, T &data, const std::string &name="none-name")
        string name = "Thread-" + to_string(i + 1);
        threads->emplace_back(func, rq, name);
        threads->back().Start();
    }
}

void StartConsumer(vector<Thread<ringqueue_t>> *threads, int num, ringqueue_t &rq)
{
    Start(threads, num, rq, Consumer);
}
void StartProductor(vector<Thread<ringqueue_t>> *threads, int num, ringqueue_t &rq)
{
    Start(threads, num, rq, Productor);
}

void WaitAllThread(vector<Thread<ringqueue_t>> &threads)
{
    for (auto &thread : threads)
    {
        thread.Join();
    }
}
int main()
{
    ringqueue_t *rq = new ringqueue_t(10);
    vector<Thread<ringqueue_t>> threads;
    // 启动线程
    StartConsumer(&threads, 4, *rq);
    StartProductor(&threads, 2, *rq);
    // 等待所有线程
    WaitAllThread(threads);

    return 0;
}